package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuServerUserData;
import com.yanqu.road.logic.pb.DouLuoDaLuPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.douluodalu.DouLuoDaLuModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_GET_USER_DATA,desc = "玩家数据")
public class DouLuoDaLuGetUserDataCmd extends AbstractCommand {

    boolean isEntry = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DouLuoDaLuProto.DouLuoDaLuGetUserDataReqMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuGetUserDataReqMsg.parseFrom(packet.getBytes());
        if(reqMsg.hasIsEnter()){
            isEntry = reqMsg.getIsEnter();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if ((ret = DouLuoDaLuMgr.canEntryActivity(player)) != 0) return;

        DouLuoDaLuProto.DouLuoDaLuGetUserDataCrossReqMsg.Builder crossReq = DouLuoDaLuProto.DouLuoDaLuGetUserDataCrossReqMsg.newBuilder();

        crossReq.setActivityId(DouLuoDaLuMgr.getActivityInfo().getActivityId());
        // 每次进入 都要同步玩家数据
        crossReq.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        // 门客总赚钱值
        crossReq.setAllPatronsEarn(player.getModule(PatronsModule.class).getTotalPatronsAbility().longValue());
        // 如果是活动期间 且 不是休息期，则同步所有的门客
        if(ActivityMgr.activityInTime(DouLuoDaLuMgr.getActivityInfo())){
            crossReq.addAllAllPatrons(DouLuoDaLuMgr.getAllPatronsBuilder(player));
        }
        crossReq.setIsEnter(isEntry);
        //现金礼包
        DouLuoDaLuServerUserData userData = player.getModule(DouLuoDaLuModule.class).getUserData();
        if(userData != null){
            int now = DateHelper.getCurrentSecond();
            for (Map.Entry<Integer,Integer> entry : userData.getTouchMallMap().entrySet()){
                if(now > entry.getValue() + DouLuoDaLuMgr.getConfig().MALL_KEEP_TIME){
                    // 超时不下发
                    continue;
                }
                int mallId = entry.getKey();
                int touchTime = entry.getValue();
                int touchTimes = userData.getTouchMallTimes(mallId);
                if(touchTimes > 0){
                    DouLuoDaLuProto.DouLuoDaLuUserMallInfo.Builder msg = DouLuoDaLuPb.parseDouLuoDaLuUserMallInfo(mallId, touchTime, touchTimes);
                    crossReq.addMallInfo(msg);
                }

            }
        }
        player.sendUnionActivityGroupPacket(Protocol.C_DOU_LUO_DA_LU_GET_USER_DATA, crossReq, crossReq.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_DOU_LUO_DA_LU_GET_USER_DATA, DouLuoDaLuProto.DouLuoDaLuGetUserDataRespMsg.newBuilder().setRet(ret));
        }
    }
}
